<template>
  <div class="app-container">
    <!-- 分页查询 -->
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-suffix=":"
      label-width="auto"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item
            :label="$t('categoryLang.categoryName')"
            prop="categoryName"
          >
            <el-input
              v-model="queryParams.categoryName"
              :placeholder="$t('categoryLang.categoryName')"
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 操作栏 -->
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleAdd"
        >
          {{ $t("add") }}
        </el-button>
        <!-- 展开所有 -->
        <el-button
          type="success"
          plain
          size="small"
          @click.native="openTable('open')"
        >
          {{ $t("expandAll") }}
        </el-button>
        <!-- 折叠所有 -->
        <el-button
          type="warning"
          plain
          size="small"
          @click.native="openTable('collapse')"
        >
          {{ $t("collapse") }}
        </el-button>
      </el-col>
    </el-row>
    <!-- 数据列表 -->
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      row-key="categoryId"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      class="mt-10"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        align="center"
        width="80"
      />
      <el-table-column
        :label="$t('categoryLang.categoryName')"
        prop="categoryName"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('categoryLang.categoryEnName')"
        prop="categoryEnName"
        align="center"
        min-width="160"
      />
      <el-table-column
        :label="$t('categoryLang.declaredMinUsdPrice') + '(USD)'"
        prop="declaredMinUsdPrice"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('categoryLang.hsCode')"
        prop="hsCode"
        align="center"
        min-width="160"
      />
      <el-table-column
        :label="$t('categoryLang.categoryLevel')"
        align="center"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-tag :type="categoryLevelDictTag[scope.row.categoryLevel]">
            {{
              $t(
                getTitleByValue(
                  categoryLevelOptionsList,
                  scope.row.categoryLevel
                )
              )
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operation')"
        width="150"
        align="center"
        fixed="right"
      >
        <template v-slot="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t("edit") }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 表单 -->
    <CategoryForm
      v-model="categoryParams.categoryId"
      :visible="categoryDialog"
      :operation="categoryParams.operation"
      :title="infoTitle"
      @close="infoClose"
    />
  </div>
</template>

<script>
import CategoryForm from "./components/info-form.vue";
import { categoryLevelDict } from "@/utils/dictData";
import { getDictOptionList, getTitleByValue } from "@/utils/dictUtils";
import { getCategoryPageList, deleteCategory } from "@/api/system/category";
export default {
  name: "Category",
  components: { CategoryForm },
  data() {
    return {
      expandRowKeys: [],
      total: 0,
      loading: false,
      infoTitle: "",
      categoryDialog: false,
      queryParams: {
        categoryName: null,
        pageNo: 1,
        pageSize: 10,
      },
      categoryParams: {
        categoryId: null,
        operation: 0,
      },
      getTitleByValue: getTitleByValue,
      categoryLevelDictTag: ["danger", "success", "info"],
      categoryLevelOptionsList: getDictOptionList(categoryLevelDict), //品类类型
      tableData: [],
      isOpen: false,
    };
  },
  created() {
    this.getCategoryPageList();
  },
  methods: {
    openTable(str) {
      if (str == "open") {
        this.isOpen = true;
      } else {
        this.isOpen = false;
      }
      this.$nextTick(() => {
        this.handleArr(this.tableData, this.isOpen);
      });
    },
    handleArr(arr, flag) {
      arr.forEach((i) => {
        this.$refs.table.toggleRowExpansion(i, flag);
        if (i.children) {
          this.handleArr(i.children);
        }
      });
    },
    // 重置
    async handleReset() {
      await this.$refs.queryParams.resetFields();
    },
    // 查询
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getCategoryPageList();
    },
    // 品类列表
    async getCategoryPageList() {
      this.loading = true;
      const { data } = await getCategoryPageList({ ...this.queryParams });
      this.loading = false;
      this.tableData = data.categoryInfoDTOList;
      this.total = data.totalRowNum;
    },
    // 新增
    async handleAdd() {
      this.categoryParams.operation = 1;
      this.categoryDialog = true;
      this.infoTitle = this.$t("add");
    },
    handleSubmit() {
      this.$refs.queryParams.submit();
    },
    // 编辑
    handleEdit(row) {
      this.categoryParams.operation = 2;
      this.categoryParams.categoryId = row.categoryId;
      this.infoTitle = this.$t("edit");
      setTimeout(() => {
        this.categoryDialog = true;
      }, 400);
    },
    // 删除
    async handleDelete(row) {
      const flag = await this.$confirm(
        this.$t("delete") + "?",
        this.$t("hint"),
        {
          type: "warning",
        }
      ).catch((err) => err);
      if (flag === "cancel") return;
      console.log("取消");
      const res = await deleteCategory({
        categoryId: row.categoryId,
      });
      if (!res) return;
      this.$message({
        message: this.$t("handleSuccess"),
        type: "success",
      });
      this.getCategoryPageList();
    },
    // 关闭弹出层
    infoClose() {
      this.categoryParams.categoryId = null;
      this.categoryParams.operation = 0;
      this.categoryDialog = false;
      this.getCategoryPageList();
    },
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage;
      this.getCategoryPageList();
    },
  },
};
</script>

<style></style>
